<?php
/**
 * Mirrors列表页
 * @version 2018-1-16
 * @version 2018-1-26 正式使用
 */
// 版本号
define('VERSION', '1.8.0');
// 程序名称
define('APPNAME', 'mirrors');
// 日志根目录
define('ROOTPATH', './');

// 引入头文件
include './pts_head.php';
// #JWTLite配置位置
// 页面访问验证配置
JWTLite::check_login();
list($loginid, $token, $loginexp) = array_values(JWTLite::$LOGIN_RESULT);

// 浏览器输入参数
$path = input('path');
$tag = input('tag');
if (!$path) {
    redirect(false, '参数有误!', 'index.php?token=' . $token);
} else if ($path == './') {
    header('location:index.php?token=' . $token);
    exit;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>mirrors - taocode</title>
        <style type="text/css">
            body{font-size:12px; color:#333;}
            /*
            table {border:solid 1px #CCC; border-top:none;border-left:none;}
            th, td{border:solid 1px #CCC; border-bottom:none;border-right:none; padding:5px 10px;}
            tr:nth-child(odd) {background-color:#efefef;}
            */
            th, td {padding:3px 100px 3px 0; text-align: left;}
            a { text-decoration: none; color:#06C}
            .nav { margin:5px 0; color:#666;}
            .nav a { color:#666;}
            .nav a:hover { color:#333;}
            .nav a:before { content: ' » ' }
            .nav a:first-child:before {content: ''}
            .nav div { color:#CCC; border-bottom:solid 1px #CCC; margin-bottom:5px;}
        </style>
    </head>
    <body>
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
        <?php
        echo '<h1>', APPNAME, '</h1>';
        // 用户登录信息
        if (isset($loginid)) {
            echo '<span title="登录过期时间：', $loginexp, ' (超时前进行跳转操作能让登录会话自动延期,避免重登录操作~)">', $loginid, '</span>';
            echo ' <a href="?action=logout" onclick="return confirm(\'您确定要注销登录吗?\');">[注销登录]</a>';
            echo '<a href="?token=', $token, '&path=', urlencode($path), '" style="font-size:12px;">[刷新]</a>';
        }
        // 面包屑导航
        FileUtil::showBreadcrumb($token, $path, $title = '位置: ');
        ?>
        <div>
            <form>
                <input type="text" name="tag" placeholder="输入标签" value="<?php echo $tag ?>" />
                <button type="submit">搜索</button>
                <button type="reset">重置</button>
                <input type="hidden" name="token" value="<?php echo $token ?>" />
                <input type="hidden" name="path" value="<?php echo $path ?>" />
            </form>
            <?php
            $filepath = '';
            $tagarr = [];
            $tagstr = '';
            $count = 0;
            $files = FileUtil::scandirReturn(ROOTPATH, 2, true, array('./index.php', './list.php', './intro.php', './search.php', './pts_head.php', './nbproject/', './.git/'));
            echo '<table border="0" cellpadding="0" cellspacing="0">';
            echo '<tr><th>名称</th><th>修改时间</th><th>预览图</th><th>标签</th><th>操作</th></tr>';
            foreach ($files as $file) {
                $pathstr = $file['path'];
                if (is_dir($pathstr)) {
                    echo '<tr style="background-color:#efefef;font-weight:bold;">';
                    echo '<td><a href="list.php?token=', $token, '&path=', urlencode($pathstr), '">', basename($pathstr), '/</a></td>';
                    echo '<td colspan="5">', date('Y-m-d H:i:s', filemtime($pathstr)), '</td>';
                    echo '</tr>';
                    foreach ($file['children'] as $file2) {
                        $pathstr2 = $file2['path']; // 标签
                        $filepath = $pathstr2 . '/tag.txt';
                        $tagarr = [];
                        if (is_file($filepath)) {
                            $tagstr = trim(file_get_contents($filepath));
                            if ($tagstr) {
                                $tagarr = explode(',', $tagstr);
                            }
                        }
                        if (!$tagarr || !in_array($tag, $tagarr)) {
                            continue;
                        }
                        echo '<tr>';
                        echo '<td>', basename($pathstr2), '</td>';
                        echo '<td>', date('Y-m-d H:i:s', filemtime($pathstr2)), '</td>';
                        // 预览图
                        $filepath = $pathstr2 . '/screenshot/0.screenshot.png';
                        echo '<td>', (is_file($filepath) ? '<a href="' . $filepath . '" target="_blank"><img src="' . $filepath . '" alt="" style="width:32px;height:32px;" /></a>' : '-'), '</td>';
                        // 标签
                        if ($tagarr) {
                            echo '<td>';
                            foreach ($tagarr as $tagid) {
                                echo '<a href="search.php?token=', $token, '&path=', urlencode($path), '&tag=', $tagid, '">', $tagid, '</a>';
                            }
                            echo '</td>';
                        } else {
                            echo '<td>-</td>';
                        }
                        echo '<td>', (is_file($filepath) ? '<a href="' . $filepath . '" target="_blank"></a>' : '-'), '</td>';
                        echo '<td>', (is_file($pathstr2 . '/intro.txt') ? '<a href="intro.php?path=' . urlencode($pathstr2) . '" target="_blank">软件简介</a>' : '-'), '</td>';
                        echo '</tr>';
                        $count++;
                    }
                }
            }
            echo '</table>';
            echo '<div style="margin-top:10px;">共查询到 <strong>', $count, '</strong> 个记录</div>';
            ?>
        </div>
    </body>
</html>
